﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace UnknowWorld.CardGame
{
    /// <summary>
    /// 卡牌容器
    /// </summary>
    public class HandCardContainer : MonoBehaviour
    {

        #region Create Card
        /// <summary>
        /// 显示反面
        /// </summary>
        public bool backSide;
        /// <summary>
        /// 可拖拽
        /// </summary>
        public bool canDrag;
        /// <summary>
        /// 预制对象
        /// </summary>
        public CardModel cardModPrefab;
        /// <summary>
        /// 卡牌起始点
        /// </summary>
        public Transform playerCardStart;
        /// <summary>
        /// 卡牌缩放
        /// </summary>
        public float cardScale = 1;
        /// <summary>
        /// 卡牌数量
        /// </summary>
        public int CardCount
        {
            get { return _cardDatas.Count; }
        }
        /// <summary>
        /// 存储card mod
        /// </summary>
        private List<CardModel> _cardDatas = new List<CardModel>();
        /// <summary>
        /// 卡牌使用时的事件
        /// </summary>
        public System.Action<bool,CardModel> WhenCardUsed { get; set; }

        /// <summary>
        /// 创建Cards
        /// </summary>
        /// <param name="cards"></param>
        public void CreateCards(params Card[] cards)
        {
            int oldLength = _cardDatas.Count;
            int length = oldLength + cards.Length;
            Vector2[] positions = SplitPosition(length);
            for (int i = 0; i < positions.Length; i++)
            {
                if (i < oldLength)
                {
                    _cardDatas[i].moveToPosition = transform.TransformPoint(positions[i]);
                }
                else
                {
                    var card = CreateCard(cards[i - oldLength], positions[i], playerCardStart.position);
                    card.SortGroup(i * 2);
                    card.canDrag = canDrag;
                    card.OnCardUsed += CarUsed;
                    _cardDatas.Add(card);
                }
            }
        }

        /// <summary>
        /// 在创建单张卡牌
        /// </summary>
        /// <param name="card"></param>
        /// <param name="position"></param>
        CardModel CreateCard(Card card, Vector3 position, Vector3 startPosition)
        {
            var cardMod = Instantiate(cardModPrefab, transform);
            cardMod.BindCard(card);
            cardMod.transform.localPosition = startPosition;
            cardMod.transform.localScale = new Vector3(cardScale, cardScale, cardScale);
            cardMod.transform.localEulerAngles = new Vector3(0, backSide ? 180 : 0, 0);
            cardMod.isFront = !backSide;
            cardMod.tag = tag;
            cardMod.moveToPosition = transform.TransformPoint(position);
            return cardMod;
        }

        /// <summary>
        /// 卡牌被使用
        /// </summary>
        void CarUsed(bool used,CardModel card)
        {
            if (used)
            {
                //删除
                for (int i = 0; i < _cardDatas.Count; i++)
                {
                    if (_cardDatas[i] == card)
                    {
                        _cardDatas.RemoveAt(i);
                        break;
                    }
                }
            }
            //重置位置
            ResetCardsPoint();
            if (null != WhenCardUsed) WhenCardUsed(used,card);
        }

        /// <summary>
        /// 重置卡牌位置
        /// </summary>
        void ResetCardsPoint()
        {
            Vector2[] points = SplitPosition(_cardDatas.Count);
            for (int i = 0; i < _cardDatas.Count; i++)
            {
                _cardDatas[i].moveToPosition = transform.TransformPoint(points[i]);
            }
        }

        #endregion

        #region Position Split
        /// <summary>
        /// 弧形半径
        /// </summary>
        //public float radius = 1;
        /// <summary>
        /// 起始结束位置
        /// </summary>
        public Vector2 startPoint, endPoint;

        /// <summary>
        /// 分割点
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        Vector2[] SplitPosition(int count)
        {
            float distance = Vector2.Distance(startPoint, endPoint);
            float splitDis = distance / (count + 1);
            Vector2[] points = new Vector2[count];
            for (int i = 0; i < count; i++)
            {
                points[i] = startPoint + new Vector2(splitDis * (i + 1), 0);
            }
            return points;
        }
        #endregion

        #region 释放数据

        /// <summary>
        /// 清空卡牌数据
        /// </summary>
        public void ClearCard()
        { 
            foreach (CardModel cardModel in _cardDatas)
            {
                Destroy(cardModel.gameObject);
            }
        }

        #endregion
    }
}